package cn.itcast.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

public class ConfigA {
    //独到的文件的值;
   // @Value("${jdbc.driver}")
   // private String drvier;
  //  @Value("${jdbc.url}")
   // private String jdbcUrl;
   // @Value("${jdbc.username}")
   // private String user;
   // @Value("${jdbc.password}")
   // private String password;

    //将DataSource载入到容器;
   // @Bean
    public DataSource getDataSource() throws PropertyVetoException {
        ComboPooledDataSource ds = new ComboPooledDataSource();
        ds.setDriverClass("com.mysql.jdbc.Driver");
        ds.setJdbcUrl("jdbc:mysql:///db1");
        ds.setPassword("root");
        ds.setUser("root");

        return ds;

    }
    //@Bean
    public DataSource getDataSource2() throws PropertyVetoException {
        DruidDataSource ds2 = new DruidDataSource();
        ds2.setUrl("jdbc:mysql:///db1");
        ds2.setUsername("root");
        ds2.setPassword("root");
        ds2.setDriverClassName("com.mysql.jdbc.Driver");


        return ds2;

    }
    //@Bean
    public Connection getConn(){
        try {
         Connection connection = getDataSource2().getConnection();
         //设置自动提交为false
         connection.setAutoCommit(false);
         return connection;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return null;

    }
}
